package pe.com.bbva.mantenimiento.dao.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pe.com.bbva.mantenimiento.dao.KitMaterialDAO;
import pe.com.bbva.mantenimiento.domain.KitMaterial;
import pe.com.bbva.util.Constantes;
import pe.com.stefanini.core.dao.GenericDAOImpl;
import pe.com.stefanini.core.exceptions.BOException;
import pe.com.stefanini.core.exceptions.DAOException;

@Service("kitMaterialDAO")
public class KitMaterialDAOImpl extends GenericDAOImpl<KitMaterial> implements KitMaterialDAO{
	
	@Autowired
	public KitMaterialDAOImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@SuppressWarnings("unchecked")
	public List<KitMaterial> findKitMateriales(KitMaterial kitMaterial)throws BOException, DAOException {
		String where = "";
		if(kitMaterial.getCantidad()!= null && !kitMaterial.getCantidad().equals("")){
			where = where + (where==""?"":" AND ") + "cantidad = '" +	kitMaterial.getCantidad() + "'";
		}
		if(kitMaterial.getDescripcion()!= null && !kitMaterial.getDescripcion().equals("")){
			where = where + (where==""?"":" AND ") + "upper(descripcion) like upper('%" + kitMaterial.getDescripcion() + "%')";
		}
		if(kitMaterial.getEstado()!= null && !kitMaterial.getEstado().equals("")){
			where = where + (where==""?"":" AND ") + "estado = '" +	kitMaterial.getEstado() + "'";
		}
		if(kitMaterial.getFechaFin()!= null && !kitMaterial.getFechaFin().equals("")){
			where = where + (where==""?"":" AND ") + "fechaFin = '" + kitMaterial.getFechaFin() + "'";
		}
		if(kitMaterial.getFechaInicio()!= null && !kitMaterial.getFechaInicio().equals("")){
			where = where + (where==""?"":" AND ") + "fechaInicio = '" +	kitMaterial.getFechaInicio() + "'";
		}
		if(kitMaterial.getKit()!= null && kitMaterial.getKit().getId()!=null){
			where = where + (where==""?"":" AND ") + "kit = '" +	kitMaterial.getKit().getId() + "'";
		}
		if(kitMaterial.getSegmento()!= null && kitMaterial.getSegmento().getId()!=null){
			where = where + (where==""?"":" AND ") + "segmento = '" +	kitMaterial.getSegmento().getId() + "'";
		}
		if(kitMaterial.getTipoMaterial()!= null && kitMaterial.getTipoMaterial().getId()!=null){
			where = where + (where==""?"":" AND ") + "tipoMaterial = '" +	kitMaterial.getTipoMaterial().getId() + "'";
		}
		if(kitMaterial.getUbigeo()!=null && kitMaterial.getUbigeo().getId()!=null){
			where = where + (where==""?"":" AND ") + "ubigeo = '" +	kitMaterial.getUbigeo().getId() + "'";
		}
		if(kitMaterial.getId()!= null && !kitMaterial.getId().equals("")){
			where = where + (where==""?"":" AND ") + "id = '" +	kitMaterial.getId() + "'";
		}
		if(!where.equals("")){
			where = Constantes.WHERE_CLAUSE + where;
		}
		String orders = " order by fechaCreacion desc";
		List<KitMaterial> listaMaterialKit = super.executeQuery(KitMaterial.class,where,orders);
		return listaMaterialKit;
	}
	
	@SuppressWarnings("deprecation")
	public Long ValidaCargaEnProceso() throws DAOException {
		Session hibernateSession = null;
		Connection con = null;
		CallableStatement cstmt =null;
		ResultSet rs  = null;		
		Long IdCarga=0L;
		try {
			hibernateSession = (Session) getHibernateTemplate().getSessionFactory().openSession();
			con = hibernateSession.connection();
			cstmt =con.prepareCall("{call TARGPS.PKG_TARGPS.PR_VALIDAR_ENPROCESO(?)}");			
			cstmt.registerOutParameter(1,oracle.jdbc.OracleTypes.VARCHAR);		
			cstmt.executeQuery();			
			IdCarga = Long.parseLong((String)cstmt.getObject(1));			
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new DAOException(e.getMessage());
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new DAOException(e.getMessage());
		}finally{
			try {
				if(rs!=null){
					rs.close();
				}
				if(cstmt!=null){
					cstmt.close();
				}
				hibernateSession.close();
			} catch (SQLException e) {
				throw new DAOException(e.getMessage());
			} catch (HibernateException e) {
				e.printStackTrace();
				throw new DAOException(e.getMessage());
			} 
		}
		return IdCarga;
	}
}